package com.tjsinfo.tjsoa.resource.duty.action;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.resource.duty.bo.TResourceDutyManager;
import com.tjsinfo.tjsoa.resource.duty.vo.TResourceDuty;
import com.tjsinfo.tjsoa.system.vo.User;

public class dutyAction extends DispatchAction {
	
	private TResourceDutyManager resourceDutyManager;
	
	public TResourceDutyManager getresourceDutyManager() {
		return resourceDutyManager;
	}

	public void setresourceDutyManager(TResourceDutyManager tResourceDutyManager) {
		this.resourceDutyManager = tResourceDutyManager;
	}

	
	/*
	 * 查询
	 */
		public ActionForward getTasks(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
			String month = request.getParameter("month");
			DateFormat sd = new SimpleDateFormat("yyyy-M-d");
			JSONArray array = new JSONArray();      //新建JSON数组对象
	        List<TResourceDuty> list=resourceDutyManager.findByLikeProperty("dutyDate",month + "-%");
	        for(TResourceDuty trd:list){
	        	JSONObject obj = new JSONObject();
	                obj.put("id", trd.getId());
	                obj.put("task", trd.getName());
	                obj.put("builddate", sd.format(sd.parse(trd.getDutyDate())));
	                obj.put("taskType", trd.getType());
	                array.put(obj);
	        }
	        response.getWriter().print(array);
			return null;
	    }
	
	/*
	 * 增加
	 */
	public ActionForward addTask(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
			String taskInfo = request.getParameter("taskInfo");
			String buildDate = request.getParameter("buildDate");
			String taskType  = request.getParameter("taskType");
			DateFormat sdf = new SimpleDateFormat("yyyy-M-d");
			Date date = sdf.parse(buildDate);
			String Date = sdf.format(date);
	          TResourceDuty trd=new TResourceDuty();
	          trd.setDutyDate(Date);
	          trd.setType(taskType);
	          trd.setName(taskInfo);
	          int tempId=resourceDutyManager.save(trd);
	        response.getWriter().print(tempId);
			return null;
	    }
	
	/*
	 * 删除
	 */
	public ActionForward delTask(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String taskId = request.getParameter("taskId");
		TResourceDuty trd=resourceDutyManager.findById(new Integer(taskId));
		resourceDutyManager.delete(trd);
		return null;
	}
	
	/*
	 * 更新
	 */
	public ActionForward updateTask(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
			String taskInfo = request.getParameter("taskInfo");
			String taskId = request.getParameter("taskId");
			TResourceDuty trd= resourceDutyManager.findById(Integer.parseInt(taskId));
			trd.setId(Integer.parseInt(taskId));			
			trd.setName(taskInfo.trim());
			resourceDutyManager.update(trd);
			return null;
			     
	       
	}
	
}
